package com.heima.article.service.impl;

import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleHomeService;
import com.heima.common.constants.article.ArticleConstants;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.common.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/8/13
 */
@Transactional
@Service
@Slf4j
public class ArticleHomeServiceImpl implements ArticleHomeService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Value("${file.oss.web-site}")
    private String webSite;

    @Override
    public ResponseResult getArticle(ArticleHomeDto articleHomeDto, Short loadType) {

        //参数默认值设置
        if (Objects.isNull(articleHomeDto.getMaxBehotTime())) {
            articleHomeDto.setMaxBehotTime(new Date());
        }
        if (Objects.isNull(articleHomeDto.getMinBehotTime())){
            articleHomeDto.setMinBehotTime(new Date());
        }

        if (Objects.isNull(articleHomeDto.getSize())){
            articleHomeDto.setSize(ArticleConstants.DEFAULT_SIZE);
        }
        //限制最大查询50条
        articleHomeDto.setSize(Math.min(articleHomeDto.getSize(),50));

        if (StringUtils.isBlank(articleHomeDto.getTag())){
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        List<Article> articleList = apArticleMapper.selectArticleList(articleHomeDto,loadType);

        ResponseResult responseResult = ResponseResult.okResult(articleList);
        responseResult.setHost(webSite);
        return responseResult;


    }
}
